package edu.dayu.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 将如下格式的excel 转为 java 实体
 * pageIndex	否	int	分页：起始条索引(从0开始，0表示第一页，默认0)
 */
public class xslBean {
    static Map<Integer, String> head = new HashMap<>();

    static List<Map<Integer,String>> list = new ArrayList<>();

    public static void main(String[] args) {
        String path = "C:\\Users\\EDY\\Desktop\\xlsbean.xlsx";

        File file = new File(path);

        EasyExcel.read(file, new AnalysisEventListener<Map>() {
            @Override
            public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
                super.invokeHeadMap(headMap, context);
                head = headMap;
            }
            @Override
            public void invoke(Map o, AnalysisContext analysisContext) {
                list.add(o);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                list.forEach(item->{
                    String name = item.get(0);
                    String str = item.get(2);
                    String tol = item.get(3);
                    if (str.equals("string")){
                        str = "String";
                    }else if (str.equals("int")){
                        str = "Integer";
                    }
                    System.out.println("@ApiModelProperty(\""+tol+"\")");
                    System.out.println("private "+str+" "+name);
                    System.out.println();
                });
            }
        }).sheet().doRead();
    }


    public static void test()  {
        People people = new People();
        people.setUserName("zhangsan");
        System.out.println(JSONObject.toJSONString(people));
    }


    @Data
    static class People{
        @JSONField(name = "user_name")
//        @JsonProperty(value = "user_name")
        private String userName;
    }
}
